package com.example.jiaju.ui.main;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.jiaju.DataApplication;
import com.example.jiaju.R;
import com.example.jiaju.adapter.FurnitureDesignAdapter;
import com.example.jiaju.adapter.FurnitureRecommendAdapter;
import com.example.jiaju.api.FurnitureApi;
import com.example.jiaju.databinding.ActivityFurnitureBinding;
import com.example.jiaju.entity.Furniture;
import com.example.jiaju.utils.MyItemDecoration;
import com.example.jiaju.utils.RetrofitUtils;
import com.gyf.immersionbar.ImmersionBar;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.RoundLinesIndicator;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FurnitureActivity extends AppCompatActivity {

    private static String TAG="FurnitureActivity";
    private Banner banner;
    private Banner banner2;

    private List<String> picList=new ArrayList<>();
    private RecyclerView arDesignRecyclerView;
    private FurnitureDesignAdapter furnitureDesignAdapter;

    private RecyclerView recommendRecyclerView;
    private FurnitureRecommendAdapter furnitureRecommendAdapter;

    private List<Furniture.PageDTO.ListDTO> list=new ArrayList<>();

    private ActivityFurnitureBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding= DataBindingUtil.setContentView(this,R.layout.activity_furniture);

        //设置黑色状态栏字体以及重叠问题
        ImmersionBar.with(this).statusBarDarkFont(true).fitsSystemWindows(true).init();

        banner=findViewById(R.id.banner);
        List<String> bannerList=new ArrayList<>();
        bannerList.add("https://starry-lixu.oss-cn-hangzhou.aliyuncs.com/202211162304481.jpg");
        bannerList.add("https://starry-lixu.oss-cn-hangzhou.aliyuncs.com/202211162305622.jpg");
        bannerList.add("https://starry-lixu.oss-cn-hangzhou.aliyuncs.com/202211162305999.jpg");

        banner.setAdapter(new BannerImageAdapter<String>(bannerList) {
            @Override
            public void onBindView(BannerImageHolder holder, String data, int position, int size) {
                Glide.with(holder.imageView)
                        .load(data)
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners( 200)))//加载圆角图片
                        .into(holder.imageView);
            }
        }).addBannerLifecycleObserver(this);
        banner.setIndicator(new RoundLinesIndicator(this));//改变轮播图的指向器的类型

        //魅族样式
        banner.setBannerGalleryEffect(30,30,10,0);

        banner2=findViewById(R.id.banner2);
        List<String> bannerList2=new ArrayList<>();
        bannerList2.add("https://starry-lixu.oss-cn-hangzhou.aliyuncs.com/202211201255822.png");
        bannerList2.add("https://starry-lixu.oss-cn-hangzhou.aliyuncs.com/202211201255028.png");
        banner2.setAdapter(new BannerImageAdapter<String>(bannerList2) {
            @Override
            public void onBindView(BannerImageHolder holder, String data, int position, int size) {
                Glide.with(holder.imageView)
                        .load(data)
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners( 20)))//加载圆角图片
                        .into(holder.imageView);
            }
        }).addBannerLifecycleObserver(this);

        Glide.with(this).load("https://starry-lixu.oss-cn-hangzhou.aliyuncs.com/202211201301236.png").into((ImageView) findViewById(R.id.img1));
        Glide.with(this).load("https://starry-lixu.oss-cn-hangzhou.aliyuncs.com/202211201302759.png").into((ImageView) findViewById(R.id.img2));

        //家具设计图片

        picList.add("https://starry-lixu.oss-cn-hangzhou.aliyuncs.com/202211292005806.png");
        picList.add("https://starry-lixu.oss-cn-hangzhou.aliyuncs.com/202211292012343.png");
        picList.add("https://starry-lixu.oss-cn-hangzhou.aliyuncs.com/202211292015308.png");
        picList.add("https://starry-lixu.oss-cn-hangzhou.aliyuncs.com/202211292016164.png");

        //网络请求三步走
        //1.获取到API接口
        FurnitureApi furnitureApi= RetrofitUtils.getRetrofit(DataApplication.HTTP_URL).create(FurnitureApi.class);
        //2.获取到实体类
        Call<Furniture> call=furnitureApi.getAllFurniture(1,5);
        //3.消息请求队列，取出数据
        call.enqueue(new Callback<Furniture>() {
            @Override
            public void onResponse(Call<Furniture> call, Response<Furniture> response) {
                Furniture furniture=response.body();
                if(furniture==null){
                    Toast.makeText(FurnitureActivity.this,"家具数据请求异常！",Toast.LENGTH_SHORT).show();
                }else {
                    list = furniture.page.list;
                    Log.i(TAG, "onResponse: " + list);
                    //家具为你推荐
                    recommendRecyclerView = findViewById(R.id.furniture_recommend_recyclerview);
                    furnitureRecommendAdapter = new FurnitureRecommendAdapter(list, FurnitureActivity.this);
                    recommendRecyclerView.setAdapter(furnitureRecommendAdapter);
                    recommendRecyclerView.addItemDecoration(new MyItemDecoration(8));
                    recommendRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
                }
            }

            @Override
            public void onFailure(Call<Furniture> call, Throwable t) {

            }
        });

        arDesignRecyclerView=findViewById(R.id.ar_design_recyclerview);
        furnitureDesignAdapter=new FurnitureDesignAdapter(picList);
        arDesignRecyclerView.setAdapter(furnitureDesignAdapter);
        arDesignRecyclerView.addItemDecoration(new MyItemDecoration(8));
        arDesignRecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));

        //回到HomeActivity
        binding.include.returnBtn.setOnClickListener(v->finish());

    }
}